﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Plane
{
    public partial class setting : Form
    {
        private MainWindow main;
        [DllImport("winmm.dll")]
        public static extern int waveOutSetVolume(IntPtr hwo, uint dwVolume);
        public Keys SelectedMoveUpKey { get; private set; }
        public Keys SelectedMoveDownKey { get; private set; }
        public Keys SelectedMoveLeftKey { get; private set; }
        public Keys SelectedMoveRightKey { get; private set; }
        public Keys SelectedFireKey { get; private set; }

        public (string Password, int Money, int HighestScore, int RecoveryAmount, int BulletAmount, string PlaneSkin, string KeyUp, string KeyDown, string KeyLeft, string KeyRight, string KeyFire, int SoundVolume) User;

        public string username;

        private MyPlane mp;

        private MyBullet mb;
        public setting(MainWindow main,(string Password, int Money, int HighestScore, int RecoveryAmount, int BulletAmount, string PlaneSkin, string KeyUp, string KeyDown, string KeyLeft, string KeyRight, string KeyFire, int SoundVolume) User,string username)
        { 
        InitializeComponent();
            trackBar1.Maximum = 100;
            trackBar1.Minimum = 0;

            // 读取配置项
            string moveUpKey = User.KeyUp;
            string moveDownKey = User.KeyDown;
            string moveLeftKey = User.KeyLeft;
            string moveRightKey = User.KeyRight;
            string fireKey = User.KeyFire;
            int volume = User.SoundVolume;

            // 初始化ComboBox显示当前按键设置
            comboBoxUp.Text = moveUpKey;
            comboBoxDown.Text = moveDownKey;
            comboBoxLeft.Text = moveLeftKey;
            comboBoxRight.Text = moveRightKey;
            comboBoxFire.Text = fireKey;

            // 初始化音量
            if (volume >= 0 && volume != 50)
            {
                trackBar1.Value = volume;
                SetSystemVolume(volume);
                label10.Text = $"{volume}%";
            }
            else
            {
                trackBar1.Value = 50;
                SetSystemVolume(50);
                label10.Text = "50%";
            }

            trackBar1.ValueChanged += trackBar1_Scroll;

            // 设置ComboBox只能通过按键输入
            foreach (var comboBox in new[] { comboBoxUp, comboBoxDown, comboBoxLeft, comboBoxRight, comboBoxFire })
            {
                comboBox.DropDownStyle = ComboBoxStyle.DropDown;
                comboBox.KeyDown += ComboBox_KeyDown;
                comboBox.KeyPress += ComboBox_KeyPress;
            }
            this.main = main;
            this.User = User;
            this.username = username;
        }

        private void ComboBox_KeyDown(object sender, KeyEventArgs e)
        {
            var comboBox = (ComboBox)sender;
            comboBox.Text = e.KeyCode.ToString();
            e.Handled = true; // 阻止进一步处理
        }

        private void ComboBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true; // 阻止字符输入
        }

        private void button1_Click(object sender, EventArgs e)
        {
            comboBoxFire.Text = "J";
            comboBoxRight.Text = "D";
            comboBoxLeft.Text = "A";
            comboBoxDown.Text = "S";
            comboBoxUp.Text = "W";
            UserDbHelper.UpdateKeyUp(username, "W");
            UserDbHelper.UpdateKeyRight(username, "D");
            UserDbHelper.UpdateKeyLeft(username, "A");
            UserDbHelper.UpdateKeyDown(username, "S");
            UserDbHelper.UpdateKeyFire(username, "J");
            UserDbHelper.UpdateSoundVolume(username, 50);
            trackBar1.Value = 50;
            SetSystemVolume(50);
            label10.Text = "50%";
            mp = new MyPlane(username);
            mb = new MyBullet(username);
            main.Show();
            this.Hide();
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            int volume = trackBar1.Value;
            SetSystemVolume(volume);
            label10.Text = $"{volume}%";
        }

        private void SetSystemVolume(int volume)
        {
            // 确保音量在0-100范围内
            volume = Math.Max(0, Math.Min(100, volume));

            // 计算音量值
            uint newVolume = ((uint)volume * 0xFFFF / 100) & 0xFFFF;
            newVolume = (newVolume << 16) | newVolume;

            waveOutSetVolume(IntPtr.Zero, newVolume);
        }

        private void button2_Click(object sender, EventArgs e)
        { 
            try
            {
                UserDbHelper.UpdateKeyUp(username, comboBoxUp.Text);
                UserDbHelper.UpdateKeyRight(username, comboBoxRight.Text);
                UserDbHelper.UpdateKeyLeft(username, comboBoxLeft.Text);
                UserDbHelper.UpdateKeyDown(username, comboBoxDown.Text);
                UserDbHelper.UpdateKeyFire(username, comboBoxFire.Text);
                UserDbHelper.UpdateSoundVolume(username,trackBar1.Value);
                MessageBox.Show("设置已保存！");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"保存设置时出错: {ex.Message}");
            }
            mp = new MyPlane(username);
            mb = new MyBullet(username);
            main.Show();
            this.Hide();
        }
    }
}